package com.example.demo.service.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.Login;
import com.example.demo.entity.requestJson.CodeLoginReq;
import com.example.demo.entity.requestJson.LoginReq;
import com.example.demo.entity.responseJson.LoginRes;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.LoginMapper;
import com.example.demo.service.ALoginService;
import com.example.demo.tools.Md5utils;
import com.example.demo.tools.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.UUID;

@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class ALoginServiceImpl implements ALoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public BackResponseEntity<LoginRes> doLogin(LoginReq loginReq){
        if (ObjectUtils.isEmpty(loginReq.getAccount()) || ObjectUtils.isEmpty(loginReq.getLoginpwd()) || ObjectUtils.isEmpty(loginReq.getRole())) {
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "抱歉，参数缺失");
        }

        String md5Pwd = Md5utils.getmd5Lowercase("qingyu" + loginReq.getLoginpwd());
        Login login = loginMapper.queryByAccount(loginReq.getAccount());
        if (ObjectUtils.isEmpty(login) || login.getLoginRole() != 0) {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1005, "抱歉，用户不存在!");
        }


        if (!login.getLoginPwd().equals(md5Pwd)) {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1006, "抱歉，用户账号或密码错误");
        }

        String token = UUID.randomUUID()
                .toString();
        LoginRes loginRes = new LoginRes();
        redisUtil.set(token + ":authors", JSON.toJSONString(login), 60 * 30);

        loginRes.setToken(token);
        loginRes.setNickname(login.getLoginNickname());
        loginRes.setImg(login.getLoginPic());
        loginRes.setAuthorId(login.getLoginId());
        return BackResponseEntity.success(ErrCodeEnum.unknown_0,loginRes,"登录成功");

    }

    @Override
    public BackResponseEntity<LoginRes> codeLogin(CodeLoginReq codeLoginReq){
        String phone = null;
        String code = null;
        Integer role = 0;

        if (!ObjectUtils.isEmpty(codeLoginReq.getAccount())) {
            phone =codeLoginReq.getAccount()
                    .trim();
        }
        if (!ObjectUtils.isEmpty(codeLoginReq.getLogincode())) {
            code =codeLoginReq.getLogincode()
                    .trim();
        }


        if(ObjectUtils.isEmpty(phone) || ObjectUtils.isEmpty(code)){

            return BackResponseEntity.success(ErrCodeEnum.unknown_4006,"参数缺失");
        }

        Login login = loginMapper.queryByAccount(phone);

        if (ObjectUtils.isEmpty(login)) {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1005, "抱歉，用户不存在!");
        }

        Object verifyDataObject = redisUtil.get(phone + "SMS:authors");

        if (!ObjectUtils.isEmpty(verifyDataObject)) {
            String verifyData = (String) verifyDataObject;
            if (!code.equals(verifyData)) {
                return BackResponseEntity.success(ErrCodeEnum.unknown_1001,"验证码错误");
            }
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1000,"验证码过期");
        }

        String token = UUID.randomUUID()
                .toString();
        LoginRes loginRes = new LoginRes();
        redisUtil.set(token + ":authors", JSON.toJSONString(login), 60 * 30);

        loginRes.setToken(token);
        loginRes.setNickname(login.getLoginNickname());
        loginRes.setImg(login.getLoginPic());
        loginRes.setAuthorId(login.getLoginId());
        return BackResponseEntity.success(ErrCodeEnum.unknown_0,loginRes,"登录成功");
    }

}
